package com.the4man.framework.util.web;

import com.the4man.framework.model.Dictionary;
import com.the4man.framework.service.DictionaryService;
import com.the4man.framework.util.DictionaryData;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
public class DictionaryListener implements ServletContextListener {
    public DictionaryListener() {
    }

    public void contextDestroyed(ServletContextEvent arg0) {
    }

    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(context);
        DictionaryService dictionaryService = (DictionaryService)appContext.getBean("dictionaryService");
        List<Dictionary> allDictionaryList = dictionaryService.getDictionary(new Dictionary());
        DictionaryData.allDictionaryList.addAll(allDictionaryList);
    }
}